package com.sl.hx.shop.ui.home.presenter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.CheckCollect;
import com.sl.hx.shop.bean.ProductDetail;
import com.sl.hx.shop.bean.ProductList;
import com.sl.hx.shop.bean.RelationBean;
import com.sl.hx.shop.bean.ShopDetail;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.activity.GoodsDetailActivity;
import com.sl.hx.shop.ui.home.contacts.GoodsDetailContacts;
import com.sl.hx.shop.ui.home.contacts.GoodsListContacts;
import com.sl.hx.shop.utils.ConfigUtil;
import com.sl.hx.shop.utils.SPUtils;
import com.sl.hx.shop.utils.ToastUtil;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.List;

import io.reactivex.Observable;

import static com.sl.hx.shop.constant.AppConfig.ID;

public class GoodsDetailPresenter extends BasePresenter<GoodsDetailContacts.view> implements GoodsDetailContacts.presenter {

    private Context mContext;

    private boolean isSucess=false;

    public GoodsDetailPresenter(Context context) {
        this.mContext = context;

    }

    public void getProductDetail(String num_iid) {
        Observable observable = ApiFactory.getInstance().getProductDetail(ApiRetrofit.getCacheControl(),num_iid,1);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<ProductDetail>(mContext) {
            @Override
            protected void _onNext(ProductDetail productDetails) {
                getView().ShowProductDetail(productDetails);
            }

            @Override
            protected void _onError(String message) {
            }
        }, false, getView(), ActivityEvent.DESTROY);
    }

    //收藏
    public void getWriteCollect(String num_iid, int i) {
        Observable observable = ApiFactory.getInstance().getWriteCollect(SPUtils.getInstance().getString(ID), num_iid, i);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {

            }

            @Override
            protected void _onError(String message) {
            }

            @Override
            public void onComplete() {
                super.onComplete();
                getView().ShowToast("");
            }
        }, true, getView(), ActivityEvent.DESTROY);
    }

    public void getShop(String nick) {
        Observable observable = ApiFactory.getInstance().getShop(ApiRetrofit.getCacheControl(),nick,1);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<ShopDetail>(mContext) {
            @Override
            protected void _onNext(ShopDetail shopDetail) {
                getView().ShowShop(shopDetail);
            }

            @Override
            protected void _onError(String message) {
            }

        }, true, getView(), ActivityEvent.DESTROY);
    }

    //相关推荐
    public void getRelation(String num_iid) {
        Observable observable = ApiFactory.getInstance().getRelation(ApiRetrofit.getCacheControl(),num_iid,1);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<RelationBean>>(mContext) {
            @Override
            protected void _onNext(List<RelationBean> relationBean) {
                getView().ShowRelation(relationBean);
                isSucess=true;
            }

            @Override
            protected void _onError(String message) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                if (!isSucess){
                    getView().showNoData();
                }

            }
        }, false, getView(), ActivityEvent.DESTROY);
    }

    public void checkCollect(String num_iid) {
        Observable observable = ApiFactory.getInstance().checkCollect(SPUtils.getInstance().getString(AppConfig.ID), num_iid);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<CheckCollect>(mContext) {
            @Override
            protected void _onNext(CheckCollect checkCollect) {
                getView().ShowCheckCollect(checkCollect);
            }

            @Override
            protected void _onError(String message) {
            }

        }, true, getView(), ActivityEvent.DESTROY);
    }

    //购买统计type 1:直接购买，2：优惠券
    public void couponCount(int type) {
        Observable observable = ApiFactory.getInstance().productCount(type, SPUtils.getInstance().getInt(SPUKey.TOKENID), AppConfig.CHANNEL);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {
            }
            @Override
            protected void _onError(String message) {
            }
        }, false, getView(), ActivityEvent.DESTROY);
    }

}
